package com.sixth.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.sixth.bean.MarketAddress;
import com.sixth.bean.MarketUser;
import com.sixth.bean.common.BasePageInfoVo;
import com.sixth.bean.common.BaseRespVo;
import com.sixth.bean.common.BaseResultVo;
import com.sixth.constant.Constant;
import com.sixth.service.WxAddressService;
import com.sixth.util.MarketInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName WxAddressController
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/11 15:02
 **/
@RestController
@RequestMapping("wx/address")
public class WxAddressController {

    @Autowired
    WxAddressService wxAddressService;

    /***
     * 显示用户的所有收货地址
     * @return BaseRespVo
     */
    @RequestMapping("list")
    public BaseRespVo list() {

        MarketUser marketUser = MarketInfoUtil.getMarketUser();

        BasePageInfoVo basePageInfoVo = wxAddressService.list(marketUser.getId());

        return BaseRespVo.ok(basePageInfoVo);
    }

    /***
     * 显示收货地址的详细信息
     * @return BaseRespVo
     */
    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        // TODO 如果直接输入其他id,是不是可以查到其他用户的消息，所以该查询要和用户id绑定
        MarketUser marketUser = MarketInfoUtil.getMarketUser();

        MarketAddress marketAddress = wxAddressService.detail(marketUser.getId(), id);

        return BaseRespVo.ok(marketAddress);
    }

    /***
     * 新增或保存修改后的地址
     * @return BaseRespVo
     */
    @RequestMapping("save")
    public BaseRespVo save(@RequestBody MarketAddress marketAddress) {

        MarketUser marketUser = MarketInfoUtil.getMarketUser();
        int code = wxAddressService.save(marketAddress, marketUser.getId());

        if (code == Constant.OK) {
            return BaseRespVo.ok(marketAddress.getId());
        }
        return BaseRespVo.error("修改失败");
    }


    /***
     * 删除收货地址
     * @return BaseRespVo
     */
    @RequestMapping("delete")
    public BaseResultVo delete(@RequestBody Map map) {

        MarketUser marketUser = MarketInfoUtil.getMarketUser();

        Integer id = (Integer) map.get("id");
        int code = wxAddressService.delete(id, marketUser.getId());

        if (code == Constant.OK) {
            return BaseResultVo.ok();
        }
        return BaseResultVo.error("删除失败");
    }


}
